package com.cdm.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.json.JsonObject;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cdm.pojo.User;
import com.cdm.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private IUserService userService;
	
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private HttpSession session;
	
	/*@RequestMapping("/showUser")
	public String toIndex(HttpServletRequest request , Model model){
		int userId = Integer.parseInt(request.getParameter("id"));
		User user = this.userService.getUserById(userId);
		model.addAttribute("user", user);
		return "showUser";
	}*/
	
	@RequestMapping("/users")
	public String toUsers(){
		return "users";
	}
	
	@RequestMapping("/login")
	public void tologinJson(HttpServletResponse response) throws Exception{
		Map<String, String> map=new HashMap<String, String>();
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		map.put("userName", userName);
		map.put("password", password);
		User user=userService.getLoginInfo(map);
		JSONObject jsonObject=new JSONObject();
		if (user!=null) {
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		jsonObject.put("user", user);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
		
	}
	
	@RequestMapping("/loginToHome")
	public String loginToHome(){
		Map<String, String> map=new HashMap<String, String>();
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		map.put("userName", userName);
		map.put("password", password);
		User user=userService.getLoginInfo(map);
		System.out.println("====="+user);
		if (user!=null) {
			session.setAttribute("User", user);
			session.setMaxInactiveInterval(1000*60*30);
			return "home";
		}else{
			request.setAttribute("loginErr", "�û�����������!");
			return "login/login";
		}
	}
	
	@RequestMapping("/registe")
	public void toRegisteJson(HttpServletResponse response) throws Exception{
		//Map<String, String> map=new HashMap<String, String>();
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String userSex = request.getParameter("userSex");
		String userPhone = request.getParameter("userPhone");
		String userEmail = request.getParameter("userEmail");
		User user = new User(userSex, userName, password, userPhone, userEmail,"image/touxiang1.png");
		//User user = new User("nan","ccc","123","1234678909","123@qq.com","image/touxiang1.png");
		JSONObject jsonObject=new JSONObject();
		if(userService.registerUser(user)){
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/getUser")
	public void getUser(HttpServletResponse response) throws Exception{
		int userId = Integer.parseInt(request.getParameter("id"));
		List<User> userList = this.userService.getUserById(userId);
		JSONObject jsonObject=new JSONObject();
		if (userList!=null) {
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		jsonObject.put("userList", userList);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/getJoiner")
	public void getJoiner(HttpServletResponse response) throws Exception{
		int activityId = Integer.parseInt(request.getParameter("activityId"));
		List<User> userlist = this.userService.getJoiner(activityId);
		JSONObject jsonObject=new JSONObject();
		if (userlist!=null) {
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		jsonObject.put("userList", userlist);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/changeUserInfo")
	public void changeUserInfo(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		String userName = request.getParameter("userName");
		String password = request.getParameter("userPassword");
		String userSex = request.getParameter("userSex");
		String userPhone = request.getParameter("userPhone");
		String userEmail = request.getParameter("userEmail");
		String imageUrl = request.getParameter("imageUrl");
		int userId = Integer.parseInt(request.getParameter("userId"));
		map.put("userName", userName);
		map.put("userPassword", password);
		map.put("userSex", userSex);
		map.put("userPhone", userPhone);
		map.put("userEmail", userEmail);
		map.put("imageUrl", imageUrl);
		map.put("userId", userId);
		Boolean sucess = userService.changeUserInfo(map);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", sucess);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/uploadImage")
	private void uploadAvatar(HttpServletResponse response)throws IOException {

		String userName=request.getParameter("userName");

		String path="C:/apache-tomcat-7.0.69/webapps/funActivityService/lib/imgs";

		String fileName=userName;

		System.out.println("头像上传路径:"+ path+ fileName);

		File file =new File(path,fileName);

		FileOutputStream fos =new FileOutputStream(file);

		byte[] buffer =new byte[1024*8];

		int len;

		while((len = request.getInputStream().read(buffer)) != -1) {

		fos.write(buffer,0, len);

		}

		boolean isSuccess =false;

		//UserBean user = biz.findUserByUserName(userName);

		//user.setAvatar(typeAvatar+"/"+ fileName);

		//isSuccess = biz.updateUser(user);

		ObjectMapper om=new ObjectMapper();

		if(isSuccess){

		om.writeValue(response.getOutputStream(),"上传头像成功");

		}else{

		om.writeValue(response.getOutputStream(),"上传头像失败");

		}

		fos.close();

		}
	
}
